<?php

namespace Admin\Controller;

class QiniuController extends CommonController
{
    public function edit()
    {
        $config['type']    = (C('FILE_UPLOAD_TYPE') === 'Qiniu') ? 1 : 0;
        $config['setting'] = C('UPLOAD_TYPE_CONFIG');
        $this->assign($config);
        $this->display();
    }

    public function save()
    {
        if (!IS_POST) $this->error('请求方式错误');
        $config['FILE_UPLOAD_TYPE'] = (I('param.type') === '1') ? 'Qiniu' : 'Local';
        if ($config['FILE_UPLOAD_TYPE'] == 'Local') {
            $config['UPLOAD_TYPE_CONFIG'] = null;
        } else {
            $setting = I('param.setting');
            // host secretKey accessKey domain bucket
            if (empty($setting['host'])) {
                $this->error('存储区域不能为空');
            }
            if (empty($setting['accessKey'])) {
                $this->error('AccessKey不能为空');
            }
            if (empty($setting['secretKey'])) {
                $this->error('SecretKey不能为空');
            }
            if (empty($setting['domain'])) {
                $this->error('域名不能为空');
            }
            if (empty($setting['bucket'])) {
                $this->error('存储名称不能为空');
            }
            $config['UPLOAD_TYPE_CONFIG'] = $setting;
        }
        $config_file = CONF_PATH . 'upload.php';
        file_put_contents($config_file, '<?php' . PHP_EOL . 'return ' . var_export($config, true) . ';');
        $this->success('保存成功');
    }
}